﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FisheryPlatform.Bussiness.Product;
using FisheryPlatform.Entity;
using FisheryPlatform.Common;
using FisheryPlatform.Bussiness;
using FisheryPlatform.Bussiness.LoginCookie;
using FisheryPlatform.Bussiness.Cache;
using FisheryPlatform.SiteConfig.FileStore;
using FisheryPlatform.Bussiness.FileStore;

namespace FisheryPlatform.Web.Admin.Product
{
    public partial class Publish : System.Web.UI.Page
    {
        protected int result = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                Save();
            }
            else
            {
                InitData();
            }
        }

        private void InitData()
        {
            long proId = 0;
            if (long.TryParse(Request.QueryString["proId"], out proId) && proId > 0)
            {
                var proInfo = ProductManager.Get(proId);
                this.txt_Title.Value = proInfo.Title;
                this.pl_Cate.SelectedItemIds = proInfo.CategoryId.ToString();
                this.pl_Area.SelectedItemIds = proInfo.AreaId.ToString();
                txt_Type.Value = proInfo.Type.ToString();
                txt_MinPrice.Value = proInfo.MinPrice.ToString();
                txt_MaxPrice.Value = proInfo.MaxPrice.ToString();
                txt_Desc.Value = proInfo.Description;
                txt_Contract.Value = proInfo.Contact;
                txt_Mobile.Value = proInfo.Mobile;
                txt_Phone.Value = proInfo.Phone;
                if (proInfo.ShowCustomerInfo == 1)
                {
                    chk_ShowCustomerInfo.Checked = true;
                }
            }
        }

        private void Save()
        {
            #region
            try
            {
                ProductEntity proInfo = new ProductEntity();
                long proId = 0;
                if (long.TryParse(Request.QueryString["proId"], out proId) && proId > 0)
                {
                    proInfo.ProductId = proId;
                }
                proInfo.Title = txt_Title.Value.Trim();
                proInfo.CategoryId = long.Parse(this.pl_Cate.SelectedItemIds);
                proInfo.AreaId = long.Parse(this.pl_Area.SelectedItemIds);
                proInfo.Type = long.Parse(txt_Type.Value);
                proInfo.MinPrice = decimal.Parse(txt_MinPrice.Value);
                proInfo.MaxPrice = decimal.Parse(txt_MaxPrice.Value);
                proInfo.Description = txt_Desc.Value.Trim();
                proInfo.Contact = txt_Contract.Value.Trim();
                proInfo.Mobile = txt_Mobile.Value.Trim();
                proInfo.Phone = txt_Phone.Value.Trim();
                proInfo.Status = ProductStatusEnum.Publishing;
                proInfo.ClientIp = IPAddressUtility.ConvertToLong(IPAddressUtility.GetClientIpFrom(Request));
                proInfo.UserId = new LoginUserInfo().UserID;
                proInfo.ShowCustomerInfo = (byte)(chk_ShowCustomerInfo.Checked ? 1 : 0);

                result = ProductManager.Save(proInfo) ? 1 : 2;

                if (proId > 0)
                {
                    ProductStaticHtmlCache.Delete(proId);
                }
            }
            catch (Exception ex)
            {
                result = 2;
                ExceptionMessage.Record(ex);
            }
            #endregion
        }
    }
}